package com.qcby.mycommunity_003.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.mycommunity_003.annotation.LogAnnotation;
import com.qcby.mycommunity_003.entity.Menu;
import com.qcby.mycommunity_003.entity.RoleMenu;
import com.qcby.mycommunity_003.form.MenuForm;
import com.qcby.mycommunity_003.service.MenuService;
import com.qcby.mycommunity_003.service.RoleMenuService;
import com.qcby.mycommunity_003.util.Result;
import com.qcby.mycommunity_003.vo.AllMenuParent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @GetMapping("/list")
    public Result MenuList(){
     List<AllMenuParent> list=menuService.Menulist();
     return Result.ok().put("data",list);
    }

    /**
     * 新增
     * @param menu
     * @return
     */
    @LogAnnotation("新增菜单")
    @PostMapping("/add")
    public Result Add(@RequestBody Menu menu){
        menuService.save(menu);
        return Result.ok();
    }

    /**
     * 回显：通过id
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result getById(@PathVariable Integer id){
        MenuForm menuForm=new MenuForm();
        Menu menu = menuService.getById(id);
        BeanUtils.copyProperties(menu,menuForm);
        menuForm.setId(menu.getMenuId());
        return Result.ok().put("data",menuForm);
    }

    /**
     * 修改
     * @param menuForm
     * @return
     */
    @LogAnnotation("修改菜单")
    @PutMapping("/edit")
    public Result update(@RequestBody MenuForm menuForm){
        Menu menu=new Menu();
        BeanUtils.copyProperties(menuForm,menu);
        menu.setMenuId(menuForm.getId());
        menuService.updateById(menu);
        return Result.ok();
    }
    @Autowired
    private RoleMenuService roleMenuService;
    @LogAnnotation("删除菜单")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Integer id){
        //1.将角色菜单表中的对应部分删除
        QueryWrapper<RoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(id!=null,"menu_id",id);
        roleMenuService.remove(queryWrapper);
        QueryWrapper<Menu> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq(id!=null,"parent_id",id);
        //删除角色菜单表中的角色对应的子菜单部分
        ArrayList<Integer> list=new ArrayList<>();
        List<Menu> list1 = menuService.list(queryWrapper2);
        for (Menu menu : list1) {
            list.add(menu.getMenuId());
        }
        //2.删除子菜单
        QueryWrapper<RoleMenu> queryWrapper3=new QueryWrapper<>();
        queryWrapper3.in(list.size()>0,"menu_id",list);
        roleMenuService.remove(queryWrapper3);
        menuService.remove(queryWrapper2);
        //.2.删除菜单
        menuService.removeById(id);
        return Result.ok();
    }
}

